<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header class="home_header">
      <div>
        <img src="../assets/css/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggeleCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router>
          <el-submenu :index="item.id.toString()" v-for="item in list" :key="item.id">
            <template slot="title">
              <i :class="infoIcons[item.id]" class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/' + childItem.path"
                          v-for="childItem in item.children"
                          :key="childItem.id"
                          :default-active="$router.history.current.path"
                          >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ childItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      list: [
      ],
      infoIcons: {
        125: 'el-icon-user',
        103: 'el-icon-folder',
        101: 'el-icon-s-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-platform'
      },
      isCollapse: false
    }
  },
  methods: {
    logout () {
      // 清空token
      window.sessionStorage.clear()
      // 跳转到登录页
      this.$router.push('/login')
    },
    handleOpen (key, keyPath) {
    },
    handleClose (key, keyPath) {
    },
    async getHomeList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return
      this.list = res.data
    },
    // 点击按钮，展开折叠菜单
    toggeleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 获取当前选中的路径，并存储
    getActivePath (activePath) {
      console.log(this.$router)
      // 存储到localstorage
      window.sessionStorage.setItem('path', activePath)
      this.activePath = activePath
    }
  },
  created () {
    this.getHomeList()
    // 赋值
    // this.activePath = window.sessionStorage.getItem('path')
  }
}
</script>

<style lang="less" scoped>
  .el-header {
    background-color: #373d41;
    padding-left: 0;
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: 0;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
  .home_container {
    height: 100%;
  }
  .home_header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
    }
  }
  .icon-font {
    margin-left: 10px;
  }
  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    letter-spacing: 0.2em;
    cursor: pointer;
    text-align: center;
    color: white;
  }
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
</style>
